
package com.catsic.zcjc.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.zcjc.condition.EquipMaintainCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.convert.ZcjcConverts;
import com.catsic.zcjc.dao.EquipMaintainDao;
import com.catsic.zcjc.entity.Equip;
import com.catsic.zcjc.entity.EquipMaintain;
import com.catsic.zcjc.entity.MaintainDataFile;
import com.catsic.zcjc.form.EquipMaintainForm;
import com.catsic.zcjc.service.EquipMaintainService;
import com.catsic.zcjc.service.EquipService;
import com.catsic.zcjc.service.MaintainDataFileService;
import com.catsic.zcjc.specifiction.EquipMaintainSpec;


@Service("equipMaintainService")
public class EquipMaintainServiceImpl extends BaseServiceImpl<EquipMaintain, String> implements EquipMaintainService{
	
	/**
	 * EquipMaintainDao:设备Dao
	 */
	@Autowired
	private EquipMaintainDao maintainDao;

	/**
	 * equipService:设备Service
	 */
	@Autowired
	private EquipService equipService;
	/**
	 * dateFileService:附件Service
	 */
	@Autowired
	private MaintainDataFileService dateFileService;
	

	@Override
	public EquipMaintain add(EquipMaintainForm form) {
		Equip equip = equipService.findById(form.getEquipId());
		EquipMaintain maintain = new EquipMaintain();
		maintain.setEquip(equip);
		maintain.setConclusion(form.getConclusion());
		maintain.setRecord(form.getRecord());
		maintain.setMaintainTime(form.getMaintainTime());
		maintain.setTheme(form.getTheme());
		maintain.setWorkmanName(form.getWorkmanName());
		maintain.setWorkmanCode(form.getWorkmanCode());
		maintain.setCreateId(form.getCreateId());
		maintain.setCreateName(form.getCreateName());
		
		super.add(maintain);
		return maintain;
	}
	@Override
	public EquipMaintainForm update(EquipMaintainForm form) {
		EquipMaintain maintain = findById(form.getId());

		maintain.setConclusion(form.getConclusion());
		maintain.setRecord(form.getRecord());
		maintain.setMaintainTime(form.getMaintainTime());
		maintain.setTheme(form.getTheme());

		return ZcjcConverts.convert(super.add(maintain));
	}


	@Override
	public EquipMaintain findById(String id){
		EquipMaintain maintain = getById(id);
		if(null == maintain){
			throw new CatsicException(CatsicExceptionCode.实体不存在, ZcjcConstants.EQUIP_MAINTAIN);
		}
		return maintain;
	}


	public Page<EquipMaintainForm> findAll(EquipMaintainCondition condition, Pageable pageable){
		EquipMaintainSpec spec = new EquipMaintainSpec(condition);
		Page<EquipMaintain> page = maintainDao.findAll(spec, pageable);
		List<EquipMaintainForm> listForm = new ArrayList<EquipMaintainForm>();
		Equip equip=null;
		for(EquipMaintain maintain :page.getContent()){
			EquipMaintainForm maintainForm=ZcjcConverts.convert(maintain);
            int theme = maintain.getTheme();
            String themeName = "";
            if(theme==0){
                themeName="创建设备";
            }else if(theme==1){
                themeName="维护设备";
            }else if(theme==2){
                themeName="停用设备";
            }else if(theme==3){
                themeName="注销设备";
            }else if(theme==4){
                themeName="报废设备";
            }
            maintainForm.setThemeName(themeName);
			if(equip == null){
				equip = equipService.findById(maintain.getEquip().getId());
			}
			maintainForm.setEquipCode(equip.getCode());
			maintainForm.setEquipName(equip.getName());
			MaintainDataFile dataFile=dateFileService.findByMaintainId(maintain.getId());
			if(dataFile != null){
				maintainForm.setAttachmentName(dataFile.getFileName());
			}
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date maintainDate=maintain.getMaintainTime();
			String maintainDateStr = df.format(maintainDate);
			maintainForm.setMaintainTimeStr(maintainDateStr);
			listForm.add(maintainForm);
	    }
	    Page<EquipMaintainForm> page_ = new PageImpl<EquipMaintainForm>(listForm,pageable,page.getTotalElements());
	    return page_;
	}


}
